import Vue from 'vue'
import { GetterTree, MutationTree, ActionTree, Module, Commit } from 'vuex'

import * as http from '@/http/cms/admin/role'

// 接口
export interface State {
  currentPage: number,
  perPage: number,
  total: number,
  roles: Array<any>,
  role: any,
  options: Array<any>
}

// 状态
const state: State = {
  currentPage: 1,
  perPage: 20,
  total: 0,
  roles: [],
  role: {},
  options: []
}

const getters: GetterTree<State, any> = {}

// 改变
const mutations: MutationTree<State> = {
  /**
   *
   * @param state
   * @param data_list
   * @param current_page
   * @param per_page
   * @param total
   */
  page(state: State, {
    data_list,
    current_page,
    per_page,
    total
  }: { data_list: any, current_page: number, per_page: number, total: number }): void {
    state.roles = data_list
    state.currentPage = current_page
    state.perPage = per_page
    state.total = total
  },
  
  /**
   * 选项
   *
   * @param state
   * @param data
   */
  options(state: State, data: any): void {
    state.options = data
  },
  
  /**
   * 增加选项
   *
   * @param state
   * @param data
   */
  appendOptions(state: State, data: any): void {
    if (state.options.length === 0) {
      state.options = [data]
    } else {
      state.options.push(data)
    }
  },
  
  /**
   * 添加
   *
   * @param state
   * @param {object} data
   */
  store(state: State, data: any): void {
    if (state.roles.length === 0) {
      state.roles = [data]
    } else {
      state.roles.push(data)
    }
  },
  
  /**
   * 显示
   *
   * @param state
   * @param {object} data
   */
  show(state: State, data: any): void {
    state.role = data
  },
  
  /**
   * 更新
   *
   * @param state
   * @param {int} index
   * @param {object} data
   */
  update(state: State, {index, data}: { index: number, data: any}): void {
    Vue.set(state.roles, index, data)
  },
  
  /**
   * 删除
   *
   * @param state
   * @param {int} index
   */
  destroy(state: State, index: number): void {
    state.roles.splice(index, 1)
  }
}

// 方法
const actions: ActionTree<State, any> = {
  /**
   * 首页
   *
   * @param commit
   * @param payload
   * @returns {Promise<any>}
   */
  index({commit}: { commit: Commit }, payload: any): Promise<any> {
    return http.index(payload.param).then(res => {
      if (res.status === 200) {
        commit('page', res.data)
      }
      
      return res
    })
  },
  
  /**
   * 查询
   *
   * @param commit
   * @param payload
   * @return {Promise<any>}
   */
  query({commit}: { commit: Commit }, payload: any): Promise<any> {
    return http.query(payload.param).then(res => {
      if (res.status === 200) {
        commit('page', res.data)
      }
      
      return res
    })
  },
  
  /**
   * 全部
   *
   * @param commit
   * @param payload
   * @return {Promise<any>}
   */
  getOptions({commit}: { commit: Commit }, payload: any): Promise<any> {
    return http.options(payload.param).then(res => {
      if (res.status === 200) {
        commit('options', res.data)
      }
      
      return res
    })
  },
  
  /**
   * 存储
   *
   * @param commit
   * @param payload
   * @returns {Promise<any>}
   */
  store({commit}: { commit: Commit }, payload: any): Promise<any> {
    return http.store(payload.param).then(res => {
      if (res.status === 200) {
        commit('store', res.data)
        commit('appendOptions', res.data)
      }
      
      return res
    })
  },
  
  /**
   * 详情
   *
   * @param commit
   * @param payload
   * @returns {Promise<any>}
   */
  show({commit}: { commit: Commit }, payload: any): Promise<any> {
    return http.show(payload.id, {}).then(res => {
      if (res.status === 200) {
        commit('show', res.data)
      }
      
      return res
    })
  },
  
  /**
   * 更新
   *
   * @param commit
   * @param id
   * @param payload
   * @returns {Promise<*>}
   */
  update({commit}: { commit: Commit }, payload: any): Promise<any> {
    return http.update(payload.id, payload.param).then(res => {
      if (res.status === 200 && res.data) {
        commit('update', {
          index: payload.index,
          data: res.data
        })
      }
      
      return res
    })
  },
  
  /**
   * 删除
   *
   * @param commit
   * @param id
   * @param payload
   * @return {Promise<*>}
   */
  destroy({commit}: { commit: Commit }, payload: any): Promise<any> {
    return http.destroy(payload.id, {}).then(res => {
      if (res.status === 200) {
        commit('destroy', payload.index)
      }
      
      return res
    })
  }
}

// 模型
export const role: Module<State, any> = {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}

export default role
